<template>
    <div class="wrapper">
        <van-cell-group inset>
            <van-field v-model="formInfo.accName" label="姓名" placeholder="请输入姓名" />
            <van-field v-model="formInfo.phoneNo" label="手机号" placeholder="请输入手机号" />
            <van-field v-model="formInfo.identityNo" label="身份证号" placeholder="请输入身份证号" />
        </van-cell-group>
        <div class="upload">
            <div class="left">
                <van-uploader :after-read="afterRead">
                    <div class="add-i" v-if="bankImg.img">
                        <img class="img" mode="widthFix" :src="bankImg.img ? bankImg.img : '@/assets/card-true.png'" />
                        <img class="camera" src="@/assets/camera-up.png" />
                    </div>
                    <div class="add-i" v-else>
                        <img class="img" mode="widthFix" src="@/assets/card-true.png" />
                        <img class="camera" v-if="!bankImg.img" src="@/assets/camera-up.png" />
                    </div>
                </van-uploader>
                <div class="b">身份证人脸页</div>
            </div>
            <div class="right">
                <van-uploader :after-read="afterRead1">
                    <div class="add-i" v-if="bankImgT.img">
                        <img class="img" mode="widthFix" :src="bankImgT.img ? bankImgT.img : '@/assets/card-false.png'" />
                        <img class="camera" src="@/assets/camera-up.png" />
                    </div>
                    <div class="add-i" v-else>
                        <img class="img" mode="widthFix" src="@/assets/card-false.png" />
                        <img class="camera" v-if="!bankImgT.img" src="@/assets/camera-up.png" />
                    </div>
                </van-uploader>
                <div class="b">身份证国徽页</div>
            </div>
        </div>
        <div class="btn" @click="uploadInfo">确 定</div>
    </div>
</template>

<script>
import { defineComponent } from 'vue'
import { commUploadImg } from '@/api/orderPos'
import { hlgUpload } from '@/api/agent'
import { showToast, showDialog  } from 'vant'

export default defineComponent({
    name: 'signing',
    setup() {
        const router = useRouter()
        const route = useRoute()
        const state = reactive({
            formInfo: {},
            bankImg: {
                img: ''
            },
            bankImgT: {
                img: ''
            },
            bankInfo: {}
        })
        state.bankInfo = JSON.parse(route.query.info)
        state.formInfo.accName = state.bankInfo.bankAccountName
        state.formInfo.phoneNo = state.bankInfo.prePhone
        state.formInfo.identityNo = state.bankInfo.identityNo
        const afterRead = file => {
            if (file.file.size > 200 * 1024) {
                //如果图片大于200K就进行压缩
                const canvas = document.createElement('canvas') // 创建Canvas对象(画布)
                const img = new Image()
                img.src = file.content // 指定图片的DataURL(图片的base64编码数据)
                img.onload = () => {
                    canvas.width = img.width / 2
                    canvas.height = img.height / 2
                    canvas.getContext('2d').drawImage(img, 0, 0, img.width / 2, img.height / 2)
                    file.content = canvas.toDataURL(file.file.type, 0.8) // 0.92为默认压缩质量
                    const files = dataURLtoFile(file.content, file.file.name)
                    // this.fileObj1 = files.file
                    console.log('压缩后', files.content)
                    commUploadImg({
                        file: files.content,
                        path: '/terminal/terminal_sale/'
                    }).then(res => {
                        const newStr = files.content.split(',')
                        state.bankImg.img = file.content
                        state.bankImg.img1 = newStr[1]
                        state.bankImg.img2 = res.object
                    })
                }
            } else {
                console.log(file.content, '未压缩')
                commUploadImg({
                    file: file.content,
                    path: '/terminal/terminal_sale/'
                }).then(res => {
                    const newStr = file.content.split(',')
                    state.bankImg.img = file.content
                    state.bankImg.img1 = newStr[1]
                    state.bankImg.img2 = res.object
                })
            }
        }
        const dataURLtoFile = (dataurl, filename) => {
            // 将base64转换为file文件
            let arr = dataurl.split(',')
            let mime = arr[0].match(/:(.*?);/)[1]
            let bstr = atob(arr[1])
            let n = bstr.length
            let u8arr = new Uint8Array(n)
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n)
            }
            let file = new File([u8arr], filename, { type: mime })
            const data = {
                //拼成你所需要传给后端的格式
                file: file,
                content: dataurl,
                message: '',
                status: ''
            }
            return data
        }
        const afterRead1 = file => {
            if (file.file.size > 200 * 1024) {
                //如果图片大于200K就进行压缩
                const canvas = document.createElement('canvas') // 创建Canvas对象(画布)
                const img = new Image()
                img.src = file.content // 指定图片的DataURL(图片的base64编码数据)
                img.onload = () => {
                    canvas.width = img.width / 2
                    canvas.height = img.height / 2
                    canvas.getContext('2d').drawImage(img, 0, 0, img.width / 2, img.height / 2)
                    file.content = canvas.toDataURL(file.file.type, 0.8) // 0.92为默认压缩质量
                    const files = dataURLtoFile(file.content, file.file.name)
                    // this.fileObj1 = files.file
                    console.log('压缩后', files.content)
                    commUploadImg({
                        file: files.content,
                        path: '/terminal/terminal_sale/'
                    }).then(res => {
                        const newStr = files.content.split(',')
                        state.bankImgT.img = file.content
                        state.bankImgT.img1 = newStr[1]
                        state.bankImgT.img2 = res.object
                    })
                }
            } else {
                console.log(file.content, '未压缩')
                commUploadImg({
                    file: file.content,
                    path: '/terminal/terminal_sale/'
                }).then(res => {
                    const newStr = file.content.split(',')
                    state.bankImgT.img = file.content
                    state.bankImgT.img1 = newStr[1]
                    state.bankImgT.img2 = res.object
                })
            }
        }
        const uploadInfo = () => {
            if (!state.formInfo.accName) {
                showToast('请输入姓名')
                return
            }
            if (!state.formInfo.phoneNo) {
                showToast('请输入手机号')
                return
            }
            if (!state.formInfo.identityNo) {
                showToast('请输入身份证号')
                return
            }
            if (!state.bankImg.img) {
                showToast('请上传身份证正面')
                return
            }
            if (!state.bankImgT.img) {
                showToast('请上传身份证反面')
                return
            }
            hlgUpload({
                ...state.formInfo,
                idcardFront: state.bankImg.img1,
                idcardBack: state.bankImgT.img1
            }).then(res => {
                showDialog ({
                    message: '报备成功',
                    title: '提示',
                    confirmButtonColor: '#66d6ff'
                }).then(() => {
                    router.go(-1)
                })
            })
        }

        return {
            ...toRefs(state),
            afterRead,
            uploadInfo,
            afterRead1
        }
    }
})
</script>

<style scoped lang="scss">
.wrapper {
    padding: 10px 0;
    box-sizing: border-box;

    .upload {
        padding: 10px;
        margin-top: 15px;
        display: flex;
        justify-content: space-between;
        .left,
        .right {
            width: 48%;
            .add-i {
                border: 1px dashed #ccc;
                border-radius: 10px;
                padding: 10px;
                box-sizing: border-box;
                position: relative;
                .camera {
                    height: 30px;
                    width: 30px;
                    position: absolute;
                    top: 50%;
                    left: 50%;
                    transform: translate(-50%, -50%);
                }
                .img {
                    width: 100%;
                    border-radius: 10px;
                }
            }

            .b {
                text-align: center;
                line-height: 30px;
            }
        }
    }
}
.btn {
    width: 150px;
    height: 40px;
    background: #66d6ff;
    opacity: 1;
    border-radius: 6px;
    margin: 50px auto;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
}
</style>
